//
//  SEGPersonalHomepageViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/10.
//

#import "SEGPersonalHomepageViewModel.h"

@interface SEGPersonalHomepageViewModel ()

@end

@implementation SEGPersonalHomepageViewModel

- (instancetype)initWithUserId:(NSString *)userId {
    self = [super init];
    if (self) {
        self.userId = userId;
        [self seg_createUserInfoCommand];
        [self seg_createUGCListCommand];
        [self seg_createProfileCommand];
    }
    return self;
}
- (void)seg_createUserInfoCommand {
    @weakify(self);
    self.userInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            [self.myManager requestGetUserInfoByUserId:self.userId Finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    [subscriber sendNext:result];
                    [subscriber sendCompleted];
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:codeType.integerValue userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            return nil;
        }];
    }];
}
- (void)seg_createUGCListCommand {
    @weakify(self);
    self.ugcListCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            [self.myManager requestNewQueryUGCByUserId:self.userId pageNo:[NSString stringWithFormat:@"%ld",(unsigned long)self.pageNo] pageLimit:@"10" Finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    [subscriber sendNext:result];
                    [subscriber sendCompleted];
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            
            return nil;
        }];
    }];
}
- (void)seg_createProfileCommand {
    self.profileCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [[SEGProfileManager sharedManager] requestProfilecompleted:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                [subscriber sendNext:@""];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
    }];
}

#pragma mark - Getter
- (SEGMyManager *)myManager {
    if (!_myManager) {
        _myManager = [[SEGMyManager alloc] init];
    }
    return _myManager;
}

@end
